package net.csdn.business.discuss.api.listener;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.event.DiscussAuditApplicationEvent;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.service.IDiscussActivityService;
import net.csdn.business.discuss.common.config.AsyncThreadPoolConfig;
import net.csdn.business.discuss.common.model.dto.SendDiscussAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussCategoryAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussCommentAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussSectionAuditDTO;
import net.csdn.business.discuss.feign.api.AuditFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @ClassName DIscussActivityListener
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/7/22 11:38
 */

@Slf4j
@Component
public class DiscussAuditListener {

    @Autowired
    private AuditFeignClient auditFeignClient;

    @EventListener
    @Async(AsyncThreadPoolConfig.ASYNC_EXECUTOR_POOL_NAME)
    public void dealDiscussActivity(DiscussAuditApplicationEvent event) {
        AuditEventDTO eventSource=(AuditEventDTO)event.getSource();
        String payload=eventSource.getPayload();
        log.info("discuss res audit send params:{}",payload);
        switch (eventSource.getAction()){
            case DiSCUSS_CREATE:
            case DiSCUSS_UPDATE:
            case DiSCUSS_DELETE:
                SendDiscussAuditDTO auditDTO=JSON.parseObject(eventSource.getPayload(),SendDiscussAuditDTO.class);
                auditFeignClient.sendDiscussAudit(auditDTO);
                break;
            case DiSCUSS_COMMENT_CREATE:
            case DiSCUSS_COMMENT_UPDATE:
            case DiSCUSS_COMMENT_DELETE:
                SendDiscussCommentAuditDTO auditCommentDTO=JSON.parseObject(eventSource.getPayload(),SendDiscussCommentAuditDTO.class);
                auditFeignClient.sendDiscussCommentAudit(auditCommentDTO);
                break;
            case DiSCUSS_CATEGORY_CREATE:
            case DiSCUSS_CATEGORY_UPDATE:
            case DiSCUSS_CATEGORY_DELETE:
                SendDiscussCategoryAuditDTO auditCategoryDTO=JSON.parseObject(eventSource.getPayload(),SendDiscussCategoryAuditDTO.class);
                auditFeignClient.sendDiscussCategoryAudit(auditCategoryDTO);
                break;
            case DiSCUSS_SECTION_CREATE:
            case DiSCUSS_SECTION_UPDATE:
            case DiSCUSS_SECTION_DELETE:
                SendDiscussSectionAuditDTO auditSectionDTO=JSON.parseObject(eventSource.getPayload(),SendDiscussSectionAuditDTO.class);
                auditFeignClient.sendDiscussSectionAudit(auditSectionDTO);
                break;
        }
    }
}
